package com.zhuhjay.app.web.controller.system;

import com.zhuhjay.common.base.controller.BaseController;
import com.zhuhjay.common.base.model.vo.HttpResult;
import com.zhuhjay.common.web.model.vo.PageInfoVo;
import com.zhuhjay.service.system.model.dto.LogPageParamDto;
import com.zhuhjay.service.system.model.vo.LogVo;
import com.zhuhjay.service.system.service.SystemLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.zhuhjay.common.base.enums.HttpCode.DELETE_SUCCESS;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Zhuhjay
 * @since 2023-02-25
 */
@Api(tags = "系统日志")
@Validated
@RestController
@RequestMapping("/log")
public class SystemLogController extends BaseController {

    @Resource
    private SystemLogService logService;

    @ApiOperation("分页查询日志")
    @PreAuthorize("@perm.hasPerm('system:manager:log:list')")
    @GetMapping("page/{pageNum}/{pageSize}")
    public PageInfoVo<LogVo> page(
            @PathVariable @Range(min = 1, max = Integer.MAX_VALUE) Integer pageNum,
            @PathVariable @Range(min = 5, max = 30) Integer pageSize,
            LogPageParamDto param) {
        return logService.logPage(pageNum, pageSize, param);
    }

    @ApiOperation("删除日志")
    @PreAuthorize("@perm.hasPerm('system:manager:log:delete')")
    @DeleteMapping("{id}")
    public HttpResult<?> deleteOne(@PathVariable Integer id) {
        logService.removeById(id);
        return HttpResult.ok(DELETE_SUCCESS);
    }

    @ApiOperation("批量删除日志")
    @PreAuthorize("@perm.hasPerm('system:manager:log:delete')")
    @DeleteMapping()
    public HttpResult<?> deleteBatch(@RequestBody List<Integer> ids) {
        if (ids.isEmpty()) {
            return HttpResult.fail("请选择要删除的日志");
        }
        logService.removeByIds(ids);
        return HttpResult.ok(DELETE_SUCCESS);
    }

}
